package com.mzpai.web.onlineutil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.common.HybridBinarizer;
import com.mzpai.util.StringUtils;

import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;

public class TestReadFile {

	public static void main(String[] args) {
		String path="D:\\pythontest\\imgs\\dog\\zhuliang\\guochan\\left\\";
		String spbh="100222";
		List<String> leftList=redaLeftImg(path,spbh);
		String fullpath=null;
		for(String str:leftList){
			fullpath=path+str;
			String tiaoxingma=decodeBarcode(fullpath);
			if(!StringUtils.isNullOrBlank(tiaoxingma)){
				System.out.println(tiaoxingma);
			}
		}
	}
	
	 /**
     * 解析读取条形码
     * @param barcodePath 条形码文件路径
     * @return 
     */
    public static String decodeBarcode(String barcodePath){
        BufferedImage image;
        com.google.zxing.Result result = null;
        try {
            image = ImageIO.read(new File(barcodePath));
            if (image != null) {
                 LuminanceSource source = new BufferedImageLuminanceSource(image);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                result = new MultiFormatReader().decode(bitmap, null);
            }
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
	
	 public static List<String> redaLeftImg(String path,String spbh){
	    	
	    	List<String> leftList=new ArrayList<String>();
	    	
	    	File file = new File(path);
	        File [] files = file.listFiles();
	        for (int i = 0; i < files.length; i++){
	            File file1 = files[i];
	            String name=file1.getName();   //根据后缀判断
	            if(name.contains(spbh)){
	            	leftList.add(name);
	            }
	        }
	        return leftList;
	    }

}
